package androidx.gdata.contacts.xml;

import androidx.gdata.XMLSerializer;
import androidx.gdata.xml.LinkUrlSerializer;
import androidx.gdata.xml.GeneratorSerializer;
import androidx.gdata.xml.OpenSearchSerializer;
import androidx.gdata.xml.CategorySerializer;
import androidx.gdata.xml.AuthorSerializer;
import androidx.gdata.LinkUrl;
import androidx.gdata.contacts.ContactFeed;
import androidx.gdata.contacts.Contact;

import java.io.PrintWriter;
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @author jennings
 *         Date: Nov 12, 2008
 */
public class ContactFeedSerializer implements XMLSerializer<ContactFeed> {
  private LinkUrlSerializer linkSerializer;
  private AuthorSerializer authorSerializer;
  private GeneratorSerializer generatorSerializer;
  private OpenSearchSerializer openSearchSerializer;
  private ContactSerializer contactSerializer;
  private CategorySerializer categorySerializer;
  
  public ContactFeedSerializer() {
    linkSerializer = new LinkUrlSerializer();
    authorSerializer = new AuthorSerializer();
    generatorSerializer = new GeneratorSerializer();
    openSearchSerializer = new OpenSearchSerializer();
    contactSerializer = new ContactSerializer();
    categorySerializer = new CategorySerializer();
  }

  public void serialize(PrintWriter out, ContactFeed contactFeed) {
    out.println(XML_PROLOG);
    out.println(FEED_PROLOG);
    out.println("  <id>" + contactFeed.getId() + "</id>");
    out.println("  <updated>" + contactFeed.getUpdated() + "</updated>");
    categorySerializer.serialize(out, contactFeed.getCategory());
    out.println("  <title type='text'>" + contactFeed.getTitle() + "</title>");
    for (LinkUrl link : contactFeed.getLinks()) {
      linkSerializer.serialize(out, link);
    }
    authorSerializer.serialize(out, contactFeed.getAuthor());
    generatorSerializer.serialize(out, contactFeed.getGenerator());
    openSearchSerializer.serialize(out, contactFeed.getOpenSearch());
    for (Contact contact : contactFeed.getEntries()) {
       contactSerializer.serialize(out, contact);
    }
    out.println(FEED_EPILOG);
  }
}